package com.example.springboot_zhenhong.controller;

import com.example.springboot_zhenhong.pojo.ResponseMessage;
import com.example.springboot_zhenhong.pojo.User;
import com.example.springboot_zhenhong.pojo.dto.UserDto;
import com.example.springboot_zhenhong.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController    //接口的方法可以返回对象，并且可以转换成json文本
@RequestMapping("/user")       //localhost:8088/user/**
public class UserController {

    @Autowired
    IUserService userService;

    //增加
    @PostMapping   //URL: localhost:8080/user    method:post
    //UserDto：项目要求严格的话，一般会使用Dto接收
    public ResponseMessage<User> add(@Validated @RequestBody UserDto  user){  //@RequestBody:默认穿进来的是一个文本，它会帮我们转成对象
        User add = userService.add(user);
        return ResponseMessage.success(add);
    }
    //查询
    @GetMapping("/{userId}")
    public ResponseMessage getUser(@PathVariable Integer userId){
        User user = userService.getUser(userId);
        return ResponseMessage.success(user);
    }
    //修改
    @PutMapping
    public ResponseMessage edit(@Validated @RequestBody UserDto  user){
        User userEdit = userService.edit(user);
        return ResponseMessage.success(userEdit);
    }


    //删除
}
